import React, { useCallback } from 'react'
import { Button } from '@tarojs/components'
import './WxAuthButton.scss'

export default function WxAuthButton({ children, onGetUserInfo }) {
  const getUserInfo = useCallback(e => {
    e.stopPropagation?.()
    // eslint-disable-next-line no-undef
    const getUserProfile = wx?.getUserProfile
    if (!getUserProfile) {
      onGetUserInfo?.()
      return
    }
    getUserProfile?.({
      desc: '用于完善用户资料',
      success: res => {
        onGetUserInfo?.(res.userInfo)
      },
      fail: () => {
        onGetUserInfo?.()
      }
    })
  }, [onGetUserInfo])
  return (
    <Button onClick={getUserInfo} className="wxAuthButton">{children}</Button>
  )
}
